package com.xudow.app.newui;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.activeshare.edu.ucenter.models.user.UserAgencyImagesWithOrther;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.newui.base.BaseUiActivity;
import com.xudow.app.ui.task.user.AgencyListTask;
import com.xudow.app.util.ImageUtils;

import java.util.List;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;

public class UserAgencyListActivity extends BaseUiActivity implements View.OnClickListener {
    @BindView(R.id.agency_gv)
    GridView agency_gv;

    @BindView(R.id.login_btn)
    TextView login_btn;


    private AgencyGvAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_agency_list);
    }

    @Override
    protected void initData() {
        adapter=new AgencyGvAdapter(this);
        agency_gv.setAdapter(adapter);
        //获取agencyList
        AgencyListTask task=new AgencyListTask(this,handler);
        addTask(task);
        task.execute();
    }
    private List<UserAgencyImagesWithOrther> data;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==AgencyListTask.SUCCESS){
                data=(List<UserAgencyImagesWithOrther>)msg.obj;
                if (null!=data&&data.size()>0){

                    adapter.setData(data);
                }else
                    getMyApp().makeToast("没找到曾就读过的机构");
            }else {
                getMyApp().makeToast(msg.getData().getString("errMessage"));
            }

        }
    };
    public void onBackBtnClick(View view){
        finish();
    }

    @Override
    protected void initListener() {
        login_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //跳转登录
        Intent intent=new Intent(this,AgencyLoginActivity.class);
        startActivity(intent);
    }

    class AgencyGvAdapter extends BaseAdapter{
        private Context context;
        private List<UserAgencyImagesWithOrther> list;

        public AgencyGvAdapter(Context context) {
            this.context = context;
        }

        public void setData(List<UserAgencyImagesWithOrther> list){
            this.list=list;
            notifyDataSetChanged();
        }
        @Override
        public int getCount() {
            if (null==list||list.size()==0)
                return 0;
            else
                return list.size();
        }

        @Override
        public UserAgencyImagesWithOrther getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View v, ViewGroup parent) {
            ViewHolder holder;
            if (null==v){
                holder=new ViewHolder();
                v=View.inflate(context,R.layout.item_agency_gv,null);
                holder.ico=(CircleImageView)v.findViewById(R.id.agency_ico);
                holder.lable=(TextView)v.findViewById(R.id.agency_lable);
                v.setTag(holder);
            }else
                holder=(ViewHolder)v.getTag();
                UserAgencyImagesWithOrther agency=list.get(position);
            if (null!=agency.getAgencyImages()) {
                String url = String.format("%s%s?name=%s", Config.BASE_URL, Config.IMAGE_LOAD_URL, agency.getAgencyImages().getThumbnail());
                ImageUtils.loadImage(context, holder.ico, url, R.mipmap.ico_school);
            }
            return v;
        }
        class ViewHolder{
            CircleImageView ico;
            TextView lable;
        }
    }
}
